package com.NPD.unimelb.chosecolor;

import java.io.Serializable;

import com.NPD.unimelb.chosecolor.po.Patient;
import com.example.chosecolor.R;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.MailTo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class ChoseColor extends Activity {
	Patient patient;
	int LEVEL;
	public int SCREENWIDTH;
	public int SCREENHEIGHT;
	ChoseColorView ccv;
	TextView textview;
	boolean gameOver = false;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		patient = (Patient) intent.getSerializableExtra("patient");
		
		//LEVEL = user.getBalloon_level();
		LEVEL = intent.getIntExtra("LEVEL", LEVEL);
		ccv = new ChoseColorView(ChoseColor.this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		//setContentView(R.layout.chosecolor);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		SCREENWIDTH = dm.widthPixels;
		SCREENHEIGHT = dm.heightPixels;
		//setContentView(R.layout.chosecolor);
		//textview = (TextView)findViewById(R.id.countdowntimer);
		setContentView(ccv);

	}
	
/*	public void reStart() {
		Intent backToBalloonView = new Intent();
		backToBalloonView.setClass(ChoseColor.this, ChoseColor.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("patient", (Serializable) patient);
		backToBalloonView.putExtras(bundle);
		startActivity(backToBalloonView);
		finish();
	}*/
	
	public boolean onTouchEvent(MotionEvent event) {
		boolean isTouched = this.ccv.onTouchEvent(event);
		return isTouched;
	}
	
	public void gameOver() {
		 
		ccv.odt.runFlag = 2;
		AlertDialog isExit = new AlertDialog.Builder(this).create();
		Log.d("gameover", "gameover = true");
		
		//if(ccv.score <= patient.chosecolor_score) {
		if(ccv.score <= 100) {
			if(ccv.time<0)
				isExit.setTitle("Time Up! Your Score is "+ccv.score);
			else
				isExit.setTitle("Wrong Mesh! Your Score is "+ccv.score);
		} else {
			isExit.setTitle("Congratulations! "+ccv.score+ "is your new highest score!");
		}
			isExit.setMessage("Would you like to try again?");
			isExit.setButton("Try Again", listener_gameover);
			isExit.setButton2("Quit", listener_gameover);
			isExit.show();
	}
	DialogInterface.OnClickListener listener_gameover = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which)
		{
			switch (which) {
			case AlertDialog.BUTTON_POSITIVE:
				recreate();
				break;
			case AlertDialog.BUTTON_NEGATIVE:
				finish();
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int KeyCode, KeyEvent event){
		if(KeyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		{
			ccv.odt.runFlag = 1;
			AlertDialog isExit = new AlertDialog.Builder(this).create();
			isExit.setTitle("Give Up!");
			isExit.setMessage("Are you sure to quit?");
			isExit.setButton("Quit", listener_quit);
			isExit.setButton2("No", listener_quit);
			isExit.show();
			
			//finish();
			
		}
		return true;
	}
	DialogInterface.OnClickListener listener_quit = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which)
		{
			switch (which) {
			case AlertDialog.BUTTON_POSITIVE:
				/*Intent backToMene = new Intent();

				backToMene.setClass(ChoseColor.this, MainMenu.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("patient", (Serializable) patient);
				bundle.putInt("LEVEL", LEVEL);
				backToMene.putExtras(bundle);
				startActivity(backToMene);*/
				finish();
				break;
			case AlertDialog.BUTTON_NEGATIVE:
				ccv.odt.runFlag = 0;
				break;
			default:
				break;
			}
		}
	};

	
}
